import { Message, type MessageConfig } from "@arco-design/web-vue";
import { isString } from "lodash-es";

type UseMessageReturn = {
  info: (customConfig: string | MessageConfig) => void;
  success: (customConfig: string | MessageConfig) => void;
  warning: (customConfig: string | MessageConfig) => void;
  error: (customConfig: string | MessageConfig) => void;
  loading: (customConfig: string | MessageConfig) => void;
  normal: (customConfig: string | MessageConfig) => void;
  clear: (position?: "top" | "bottom") => void;
};

export default function useMessage(): UseMessageReturn {
  const config: Omit<MessageConfig, "content"> = {
    duration: 1500,
    resetOnHover: true,
  };

  const info = (customConfig: string | MessageConfig) => {
    if (isString(customConfig)) {
      Message.info({
        content: customConfig,
        ...config,
      });
    } else Message.info(customConfig);
  };

  const success = (customConfig: string | MessageConfig) => {
    if (isString(customConfig)) {
      Message.success({
        content: customConfig,
        ...config,
      });
    } else Message.success(customConfig);
  };

  const warning = (customConfig: string | MessageConfig) => {
    if (isString(customConfig)) {
      Message.warning({
        content: customConfig,
        ...config,
      });
    } else Message.warning(customConfig);
  };

  const error = (customConfig: string | MessageConfig) => {
    if (isString(customConfig)) {
      Message.error({
        content: customConfig,
        ...config,
      });
    } else Message.error(customConfig);
  };

  const loading = (customConfig: string | MessageConfig) => {
    if (isString(customConfig)) {
      Message.loading({
        content: customConfig,
        ...config,
      });
    } else Message.loading(customConfig);
  };

  const normal = (customConfig: string | MessageConfig) => {
    if (isString(customConfig)) {
      Message.normal({
        content: customConfig,
        ...config,
      });
    } else Message.normal(customConfig);
  };

  return {
    info,
    success,
    warning,
    error,
    loading,
    normal,
    clear: Message.clear,
  };
}
